(function () {
	angular
	.module('ydApp')
	.directive('messageList', function () {
		return {
			restrict: "EA",
			templateUrl: 'directive/messageList/messageList.html',
			replace: true,
			require: "ngModel",
			scope: {
				call: "=",
				docHead: "=",
				head: '=',
				topTitle: '='
			},
			controller: function ($scope, localStore, $state, $ionicModal) {
				var vm = $scope.vm = {};
				vm.user = localStore.get('user');
				vm.userType = localStore.get("userType");
				//return '/img/icon-hos.jpg';
				vm.isPost = $state.current.name == 'post-chat';
				vm.avatarImg = function (userId) {
					if (userId) {
						return config.api + "HealthInquiry/" + vm.user.headImgUrl;
					} else {
						if (vm.userType == 2) {
							return config.api + "HealthInquiry/" + $scope.head;
						} else {
							return config.api + "HealthInquiry/" + $scope.docHead;
						}
					}
				}
				vm.downloadImg  = function (img) {
					return config.api + "HealthInquiry/" + img;
				}
				var maxHeight = 200;
				vm.loadImg = function (msg) {
					(function (load) {
						load(msg);
					})(function (m) {
						var img = new Image();
						img.src = config.api + "HealthInquiry/" + m.content;
						img.onload = function () {
							var winWidth = (window.screen.width-10) * 0.83 - 32-50;
							var width = img.width;
							var height = img.height;
							if (width > winWidth) {
								width = winWidth;
								console.log(width)
								height = (width/img.width)*height;
								if (height > maxHeight) {
									height = maxHeight;
									width = (height/img.height)*img.width;
								}
							} 
							m.imgWidth = width;
							m.imgHeight = height;
							m.chatImg = img.src;
						}
					})
				}
				vm.ipData = {}
				$scope.showImages = function (img) {
					vm.ipData.images = [img];
					vm.ipData.showImage(0);
				}
				
			},
			link: function ($scope, elem, attr, ngModel) {
				var vm = $scope.vm;
				ngModel.$render = function () {
					vm.msgs = ngModel.$viewValue;
				}
			}
		};
	})
})();